<template>
 <div class="login_container">
   <div class="login_box">
     <!-- 头像区域 -->
     <div class="avatar_box">
       <img src="../assets/logo3.png" alt="">
     </div>
     <div class="form">
       <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="账号:" prop="user_name">
           <el-input v-model="form.user_name" placeholder="请输入账号" prefix-icon="el-icon-user-solid"></el-input>
         </el-form-item>
         <el-form-item label="密码:" prop="user_password">
           <el-input v-model="form.user_password" placeholder="请输入密码" prefix-icon="el-icon-platform-eleme" type="password"></el-input>
         </el-form-item>
         <el-form-item v-if="isRegist" label="确认密码" prop="user_password2">
            <el-input v-model="form.user_password2" placeholder="请输入确认密码" prefix-icon="el-icon-eleme" type="password"></el-input>
         </el-form-item>
         <el-form-item>
           <el-button type="primary"  @click="login">登陆</el-button>
           <el-button type="primary"  @click="regist">注册</el-button>
						<el-link type="primary"><router-link to="/LoginByPhone">手机免密登陆</router-link></el-link>
         </el-form-item>
       </el-form>
     </div>
   </div>
 </div>
</template>

<script >
export default{
  name: 'login',
  data () {
    return {
      isLogin: true,
      isRegist: false,
      form: {
        user_name: '',
        user_password: '',
        user_password2: ''
      },
      rules: {
        user_name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ],
        user_password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ],
        user_password2: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  methods: {

    login () {
      if (this.isRegist) {
        // 清楚校验结果
        this.$refs.form.clearValidate()
        this.isRegist = false
      } else {
        // 去除确认密码的校验
        this.rules.user_password2[1].require = false
        this.$refs.form.validate((valid) => {
          if (valid) {
            console.log('表单校验成功')
            this.$store.dispatch('Login', this.form).then(() => {
              console.log('准备跳转')
              this.$router.push('/home')
            }).catch(e => {
              console.log('出错')
            })
          }
        })
      }
    },

    regist () {
      if (this.isRegist) {
        // 添加确认密码的校验
        this.rules.user_password2[1].require = true
        // 表单验证后发起请求
        this.$refs.form.validate((valid) => {
          if (valid) {
            console.log('表单校验成功')
            this.$store.dispatch('Register', this.form).then(() => {
              console.log('准备跳转')
              this.$router.push('/home')
            }).catch(e => {

            })
          }
        })
      } else {
        this.$refs.form.clearValidate()
        this.isRegist = !this.isRegist // 把隐藏的输入框显示
      }
    }
  }
}

</script>

<style scoped>
  .login_container{
    /* background-color: #2b4b6b; */
    background:url('../assets/beijing2.jpg') no-repeat;
    background-size: 100% auto;
    width:100%;
    height:100%;
  }
  .login_box{
    position: absolute;
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 3px;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
  }
  .avatar_box img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
  .form{
    margin: 20% 10px ;
  }

</style>
